﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wallpaper.Models.Entities;

namespace Wallpaper.Data.Configurations
{
    public class UserFavoritesConfiguration : IEntityTypeConfiguration<UserFavorite>
    {
        public void Configure(EntityTypeBuilder<UserFavorite> builder)
        {
            // 设置表名
            builder.ToTable("user_favorites");

            // 配置主键
            builder.HasKey(uf => uf.Id);
            builder.Property(uf => uf.Id)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("收藏记录主键ID");

            // 用户ID配置
            builder.Property(uf => uf.UserId)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("用户ID");

            // 壁纸ID配置
            builder.Property(uf => uf.WallpaperId)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("被收藏的壁纸ID");

            // 收藏时间配置
            builder.Property(uf => uf.CreatedAt)
                .IsRequired()
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasComment("收藏时间");

            // 创建唯一约束，防止用户重复收藏同一壁纸
            builder.HasIndex(uf => new { uf.UserId, uf.WallpaperId })
                .IsUnique()
                .HasDatabaseName("idx_userfavorites_user_wallpaper_unique");


            // 创建查询索引
            builder.HasIndex(uf => uf.UserId)
                .HasDatabaseName("idx_userfavorites_userid");

            builder.HasIndex(uf => uf.WallpaperId)
                .HasDatabaseName("idx_userfavorites_wallpaperid");


            builder.HasIndex(uf => uf.CreatedAt)
                .HasDatabaseName("idx_userfavorites_createdat");

        }
    
    }
}
